﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Gift.DBUtility;//Please add references
namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类:DALDepartment
    /// </summary>
    public partial class DALDepartment
    {
        public DALDepartment()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("Id", "tbDepartment");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbDepartment");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)			};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Gift.Model.tbDepartment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbDepartment(");
            strSql.Append("DepName,DepCode,DepTel,DepManager,ManagerMobile,IsDeleted,CompanyId)");
            strSql.Append(" values (");
            strSql.Append("@DepName,@DepCode,@DepTel,@DepManager,@ManagerMobile,@IsDeleted,@CompanyId)");
            SqlParameter[] parameters = {
					new SqlParameter("@DepName", SqlDbType.NVarChar,50),
					new SqlParameter("@DepCode", SqlDbType.NChar,10),
					new SqlParameter("@DepTel", SqlDbType.NVarChar,100),
					new SqlParameter("@DepManager", SqlDbType.NVarChar,50),
					new SqlParameter("@ManagerMobile", SqlDbType.NVarChar,50),
					new SqlParameter("@IsDeleted", SqlDbType.Bit,1),
                    new SqlParameter("@CompanyId", SqlDbType.Int,4),               
                                        };
            parameters[0].Value = model.DepName;
            parameters[1].Value = model.DepCode;
            parameters[2].Value = model.DepTel;
            parameters[3].Value = model.DepManager;
            parameters[4].Value = model.ManagerMobile;
            parameters[5].Value = model.IsDeleted;
            parameters[6].Value = model.CompanyId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gift.Model.tbDepartment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbDepartment set ");
            strSql.Append("DepName=@DepName,");
            strSql.Append("DepCode=@DepCode,");
            strSql.Append("DepTel=@DepTel,");
            strSql.Append("DepManager=@DepManager,");
            strSql.Append("ManagerMobile=@ManagerMobile,");
            strSql.Append("IsDeleted=@IsDeleted");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@DepName", SqlDbType.NVarChar,50),
					new SqlParameter("@DepCode", SqlDbType.NChar,10),
					new SqlParameter("@DepTel", SqlDbType.NVarChar,100),
					new SqlParameter("@DepManager", SqlDbType.NVarChar,50),
					new SqlParameter("@ManagerMobile", SqlDbType.NVarChar,50),
					new SqlParameter("@IsDeleted", SqlDbType.Bit,1),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.DepName;
            parameters[1].Value = model.DepCode;
            parameters[2].Value = model.DepTel;
            parameters[3].Value = model.DepManager;
            parameters[4].Value = model.ManagerMobile;
            parameters[5].Value = model.IsDeleted;
            parameters[6].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbDepartment set IsDeleted = 1 ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)			};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbDepartment ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbDepartment GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,DepName,DepCode,DepTel,DepManager,ManagerMobile,IsDeleted,CompanyId from tbDepartment ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)			};
            parameters[0].Value = Id;

            Gift.Model.tbDepartment model = new Gift.Model.tbDepartment();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DepName"] != null && ds.Tables[0].Rows[0]["DepName"].ToString() != "")
                {
                    model.DepName = ds.Tables[0].Rows[0]["DepName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["DepCode"] != null && ds.Tables[0].Rows[0]["DepCode"].ToString() != "")
                {
                    model.DepCode = ds.Tables[0].Rows[0]["DepCode"].ToString();
                }
                if (ds.Tables[0].Rows[0]["DepTel"] != null && ds.Tables[0].Rows[0]["DepTel"].ToString() != "")
                {
                    model.DepTel = ds.Tables[0].Rows[0]["DepTel"].ToString();
                }
                if (ds.Tables[0].Rows[0]["DepManager"] != null && ds.Tables[0].Rows[0]["DepManager"].ToString() != "")
                {
                    model.DepManager = ds.Tables[0].Rows[0]["DepManager"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ManagerMobile"] != null && ds.Tables[0].Rows[0]["ManagerMobile"].ToString() != "")
                {
                    model.ManagerMobile = ds.Tables[0].Rows[0]["ManagerMobile"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsDeleted"] != null && ds.Tables[0].Rows[0]["IsDeleted"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsDeleted"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsDeleted"].ToString().ToLower() == "true"))
                    {
                        model.IsDeleted = true;
                    }
                    else
                    {
                        model.IsDeleted = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["CompanyId"] != null && ds.Tables[0].Rows[0]["CompanyId"].ToString() != "")
                {
                    model.CompanyId = int.Parse(ds.Tables[0].Rows[0]["CompanyId"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,DepName,DepCode,DepTel,DepManager,ManagerMobile,IsDeleted ");
            strSql.Append(" FROM tbDepartment where isnull(IsDeleted,0)=0 ");
            if (strWhere.Trim() != "")
            {
                strSql.Append( strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,DepName,DepCode,DepTel,DepManager,ManagerMobile,IsDeleted ");
            strSql.Append(" FROM tbDepartment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbDepartment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Id desc");
            }
            strSql.Append(")AS Row, T.*  from tbDepartment T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbDepartment";
            parameters[1].Value = "Id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

